package com.tourlaid.mamagerment.ao.wx.impl

import com.github.pagehelper.Page
import com.github.pagehelper.PageHelper
import com.tourlaid.mamagerment.ao.wx.PersonHomeAo
import com.tourlaid.mamagerment.entity.ResponseData
import com.tourlaid.mamagerment.entity.Thought
import com.tourlaid.mamagerment.service.AttentionService
import com.tourlaid.mamagerment.service.ThoughtService
import com.tourlaid.mamagerment.service.TraceService
import com.tourlaid.mamagerment.util.DateUtil
import com.tourlaid.mamagerment.util.LoginUtil
import com.tourlaid.mamagerment.vo.HomeVo
import com.tourlaid.mamagerment.vo.ThoughtVo
import org.springframework.beans.BeanUtils
import org.springframework.stereotype.Service
import org.springframework.util.CollectionUtils
import org.springframework.util.StringUtils
import java.util.*
import javax.annotation.Resource

/**
 * Created by xiaoyongge on 2017/8/15.
 */
@Service("personHome")

class PersonHomeAoImpl : PersonHomeAo {

    @Resource
    val attentionService: AttentionService? = null
    @Resource
    val thoughtService: ThoughtService? = null
    @Resource
    val traceService : TraceService?= null

    override fun queryThought(targetUserId: Long, pageNo: Int): ResponseData<MutableList<ThoughtVo>> {
        var list: MutableList<ThoughtVo> = mutableListOf()
        var responseData = ResponseData(list)
        PageHelper.startPage<Thought>(pageNo, 10, true)
        var page = thoughtService!!.queryByUserId(targetUserId) as Page<Thought>
        responseData.totalCount = page.getTotal()
        var resultList = page.getResult()
        if (CollectionUtils.isEmpty(resultList)) {
            return responseData
        }
        val today = DateUtil.getFormantDate(Date(), DateUtil.DATA_FORMANT3)
        for (result in resultList) {
            var imageList: MutableList<String> = mutableListOf()
            var thoughtVo = ThoughtVo()
            BeanUtils.copyProperties(result, thoughtVo)
            val date = DateUtil.getFormantDate(result.createTime, DateUtil.DATA_FORMANT3)
            if(today!!.equals(date)){
                thoughtVo.date = "今天"
            }else{
                var dateArray = date.split("-")
                thoughtVo.year = dateArray[0]
                thoughtVo.month = dateArray[1]
                thoughtVo.day = dateArray[2]
            }
            if (!StringUtils.isEmpty(result.imageOne)) {
                imageList.add(result.imageOne!!)
            }
            if (!StringUtils.isEmpty(result.imageTwo)) {
                imageList.add(result.imageTwo!!)
            }
            if (!StringUtils.isEmpty(result.imageThree)) {
                imageList.add(result.imageThree!!)
            }
            if (!StringUtils.isEmpty(result.imageFour)) {
                imageList.add(result.imageFour!!)
            }
            if (!StringUtils.isEmpty(result.imageFive)) {
                imageList.add(result.imageFive!!)
            }
            thoughtVo.imageList = imageList
            list.add(thoughtVo)
        }
        return responseData
    }

    override fun getAttention(targetUserId: Long): Int {
        val attention = attentionService!!.getByUserIdAndTarget(LoginUtil.userId, targetUserId)
        if (attention == null) return 0
        return 1
    }

    override fun attention(targetUserId: Long): Int {
        return attentionService!!.attention(targetUserId)
    }

    override fun cancelAttention(targetUserId: Long) {
        attentionService!!.cancelAttention(targetUserId)
    }

    override fun queryTrace(targetUserId: Long): MutableList<MutableMap<String, String>> {
        var resultList: MutableList<MutableMap<String, String>> = mutableListOf()
        val list=  traceService!!.queryTraceMap(targetUserId)
        if(CollectionUtils.isEmpty(list)){
            return resultList
        }
        for(map in list){
            val date = DateUtil.getFormantDate(map.get("create_time") as Date, DateUtil.DATA_FORMANT3)
            var mutableMap = mutableMapOf(
                    "image" to  map.get("image") as String,
                    "date" to date,
                    "storeName" to map.get("store_name") as String)
            resultList.add(mutableMap)
        }
        return resultList
    }
}